<?php

namespace Common\Model;

use Think\Model;

class RegionModel extends Model {

    protected $_validate = array(/* array(验证字段1,验证规则,错误提示,[验证条件,附加规则,验证时间]) */
        array('name', 'require', '名称不能为空'),
        array('name', '', '名称已存在', 0, 'unique'),
    );  // 自动验证定义
    

    public function addRegion($data = '') {
        $rt = false;

        if ($this->create($data)) {
            $rt = $this->add();
        }
        return $rt;
    }

    public function updateRegion($data = '') {
        $rt = false;

        if ($this->create($data)) {
            $rt = $this->save();
        }
        return $rt;
    }

    public function deleteRegion($regionID) {
        if ($regionID <= 0) {
            $this->error = "无效ID";
            return false;
        }
        return $this->where("regionID=$regionID")->delete();
    }

    public function getRegionById($regionID) {
        if ($regionID <= 0) {
            $this->error = "无效";
            return false;
        }
        return $this->where("regionID=$regionID")->find();
    }

    public function getRegionList($pageNo = 1, $pageSize = 15, $where = array("level"=>1), $order = array()) {
		
        $rt = $this->where($where)->page("$pageNo,$pageSize")->order($order)->select();
		
		foreach($rt AS $key=>$value){
			
			$region_parent = $this->where("regionID=".$value['parentID'])->find();
			
			if($region_parent)
				$rt[$key]['parent_name'] = $region_parent['name'];
		}
        return $rt;
    }
	

    public function getRegionListLength($where = array()) {
        $count = $this->where($where)->count();
        return $count;
    }

}

?>